package com.kai.steering.data.service.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kai.steering.data.service.entity.dto.EmailRequest;
import com.kai.steering.data.service.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String fromEmail;
    @Value("${configuration.emailAddress}")
    private String emailAddress;
    @Value("${configuration.defaultEmailAddress}")
    private String defaultEmailAddress;

    /**
     * 发送简单文本邮件
     *
     * @param to      收件人邮箱地址
     * @param subject 邮件主题
     * @param text    邮件正文
     */
    public void sendSimpleEmail(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        message.setFrom(fromEmail);
        mailSender.send(message);
    }

    @Override
    public void sendSimpleEmail(EmailRequest emailRequest) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail);
        message.setSubject(emailRequest.getSubject());
        message.setText(emailRequest.getText());
        message.setTo(emailRequest.getTo().toArray(new String[0]));
        mailSender.send(message);
    }

    public void sendSimpleEmail(String subject, String text) {

        try (BufferedReader reader = new BufferedReader(new FileReader(emailAddress))) {
            String line;
            ArrayList<String> emailRequestTos = new ArrayList<>();
            while ((line = reader.readLine()) != null) {
                emailRequestTos.add(line);
            }
            if (emailRequestTos.size()<=0){
                throw new Exception("邮件配置文件中没有数据");
            }
            EmailRequest emailRequest = new EmailRequest(emailRequestTos, subject, text);

            emailRequest.setTo(emailRequestTos);
            sendSimpleEmail(emailRequest);

        } catch (Exception e) {
            e.printStackTrace();
            // 使用StringWriter和PrintWriter捕获堆栈跟踪信息
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
            String txt = stringWriter.toString();
            String result = txt + "\n" + "____________________________________________________" + "\n" + text;
            // 将捕获的堆栈跟踪信息转换为字符串
            sendSimpleEmail(defaultEmailAddress, "邮件配置错误",result );
        }
    }

    public void sendSimpleEmail(String subject,String describe, Exception exception) {

        exception.printStackTrace();
        // 使用StringWriter和PrintWriter捕获堆栈跟踪信息
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        exception.printStackTrace(printWriter);
        String result=describe+ "\n" + "____________________________________________________" + "\n" +stringWriter.toString();

        sendSimpleEmail(subject, result);
    }
}